package com.example.onion.hxchat.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.databean.FriendBean;
import com.example.onion.hxchat.activity.FriendApplyNotificationActivity;
import com.example.onion.hxchat.room.entity.Friend;
import com.example.superyi.myteam.R;

import java.util.ArrayList;
import java.util.List;
/**
 * 自然就是显示好友子项布局用到的适配器
 */
public class MyFriendAdapter extends RecyclerView.Adapter<MyFriendAdapter.ViewHolder> {

    private List<Friend> mList = new ArrayList<>();
    private Context mContext;
    private DataGetterListener mDataGetterListener;

    public void setmList(List<Friend> mList) {
        this.mList = mList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView friendImg;
        TextView nameText;
//        TextView infoText;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            friendImg = itemView.findViewById(R.id.friend_img_wc);
            nameText = itemView.findViewById(R.id.friend_name_wc);
//            infoText = itemView.findViewById(R.id.friend_info);
        }
    }

    public MyFriendAdapter(Context context, List<Friend> mList) {
        mContext = context;
        this.mList = mList;
    }

    public MyFriendAdapter() {
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载子项的布局文件，得到视图，传入ViewHolder,套路写法
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final Friend friendBean = mList.get(position);
        holder.friendImg.setImageResource(R.drawable.user);
        holder.nameText.setText(friendBean.getUserName());


//        holder.infoText.setText(friendBean.getInfo());
        //适配器只负责得到数据，跳转页面由从适配器获取到数据的Activity或者Fragment去选择跳转到哪个页面
        if (mDataGetterListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //int position = holder.getAdapterPosition();
                    //FriendBean friendBean = mList.get(position);
                    mDataGetterListener.getItemData(friendBean);
                    //跳转的选择由Activity或者Fragment去决定
//                    Intent intent = new Intent(v.getContext(), ScanUserActivity.class);
//                    intent.putExtra("friendBean", friendBean);
//                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //不是在Activity中进行跳转，需要添加这个方法
//                    mContext.startActivity(intent);
                    //Toast.makeText(v.getContext(),"点击了"+friendBean.getName(),Toast.LENGTH_SHORT).show();
                }
            });
            holder.friendImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //int position = holder.getAdapterPosition();
                    //FriendBean friendBean = mList.get(position);
                    //适配器只负责得到数据吧，跳转页面由从适配器获取到数据的Activity或者Fragment去选择跳转到哪个页面
                    mDataGetterListener.getItemData(friendBean);
                    //                    Intent intent= new Intent(v.getContext(), ScanUserActivity.class);
//                    intent.putExtra("friendBean",friendBean);
//                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //不是在Activity中进行跳转，需要添加这个方法
//                    mContext.startActivity(intent);
                    //Toast.makeText(v.getContext(),"点击了"+friendBean.getName(),Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface DataGetterListener {//适配器内创建一个回调接口，获取每一行recyclerview的相关数据

        void getItemData(Friend friendBean);//将本页面的获得的infoBean数据通过接口回调给调用方
    }

    public void setDataGetterListener(DataGetterListener dataGetterListener) {
        this.mDataGetterListener = dataGetterListener;
    }

}

